#pragma once


class CWordUnitUI : public CContainerUI
{
public:
    CWordUnitUI(CContainerUI* pParent)
    {
        CDialogBuilder builder;
        CContainerUI* pWordUnit = static_cast<CContainerUI*>(builder.Create(_T("ws_unit_item.xml"), NULL, NULL, NULL, pParent));
        if (pWordUnit)
        {
            //this->Add(pWordUnit);
            if (pParent)
            {
                pParent->Add(pWordUnit);
            }
        }
        else
        {
            //this->RemoveAll();
            if (pParent)
            {
                pParent->RemoveAll();
            }
            return;
        }
    }
    LPCTSTR GetClass() const
    {
        return _T("WordUnit");
    }

    LPVOID GetInterface(LPCTSTR pstrName)
    {
        if (_tcscmp(pstrName, _T("WordUnit")) == 0) return static_cast<CWordUnitUI*>(this);
        return CContainerUI::GetInterface(pstrName);
    }

};

class CUnitListUI : public CTileLayoutUI
{
public:
    CUnitListUI()
    {
    }


    LPCTSTR GetClass() const
    {
        return _T("UnitList");
    }

    LPVOID GetInterface(LPCTSTR pstrName)
    {
        if (_tcscmp(pstrName, _T("UnitList")) == 0) return static_cast<CUnitListUI*>(this);
        return CContainerUI::GetInterface(pstrName);
    }

    void AddUnitItem(LPCTSTR lpUnitName, LPCTSTR lpProgress, LPCTSTR lpWordCount, LPCTSTR lpUserData)
    {
        CContainerUI* pUnit = NULL;
        if( !m_dlgItemBuilder.GetMarkup()->IsValid() ) 
        {
            pUnit = static_cast<CContainerUI*>(m_dlgItemBuilder.Create(_T("ws_unit_item.xml"), (UINT)0));
        }
        else
        {
             pUnit = static_cast<CContainerUI*>(m_dlgItemBuilder.Create());
        }

        if (pUnit != NULL)
        {
            pUnit->SetName(_T("WordUnit"));
            pUnit->GetItemAt(0)->SetText(lpUnitName);
            pUnit->GetItemAt(0)->SetUserData(lpUserData);
            pUnit->GetItemAt(1)->SetText(lpProgress);
            pUnit->GetItemAt(2)->SetText(lpWordCount);
            //pUnit->SetUserData(lpUserData);
            this->Add(pUnit);
        }
    }

public:
    CDialogBuilder m_dlgItemBuilder;
};


class CWordSampleSentListUI : public CListUI
{
public:
    CWordSampleSentListUI(CPaintManagerUI* pPaintManager)
    {
        m_pPaintManager = pPaintManager;
    }

    LPCTSTR GetClass() const
    {
        return _T("SampleSentList");
    }

    LPVOID GetInterface(LPCTSTR pstrName)
    {
        if (_tcscmp(pstrName, _T("SampleSentList")) == 0) return static_cast<CWordSampleSentListUI*>(this);
        return CContainerUI::GetInterface(pstrName);
    }

public:
    void ShowSampleSent(TUnitWord* pUnitWord)
    {
        RemoveAll();
        CVerticalLayoutUI* pRowItem = NULL;
        if( !m_dlgItemBuilder.GetMarkup()->IsValid() ) 
        {
            pRowItem = static_cast<CVerticalLayoutUI*>(m_dlgItemBuilder.Create(_T("ws_word_sent.xml"), (UINT)0));
        }
        else
        {
            pRowItem = static_cast<CVerticalLayoutUI*>(m_dlgItemBuilder.Create());
        }

        if (!pUnitWord->vectSSents.empty() && pUnitWord->vectSSents.size() > 0)
        {
            for (int i = 0; i < pUnitWord->vectWordMeanings.size(); ++i)
            {
                TWordMeaningItem* pMeaning = &pUnitWord->vectWordMeanings[i];  
                for (int j = 0; j < pUnitWord->vectSSents.size(); ++j)
                {
                    TSSentItem* pSSentItem = &pUnitWord->vectSSents[j];
                    if (0 == _tcscmp(pMeaning->szType, pSSentItem->szType))
                    {
                        CDuiString strValue;
                        strValue.SmallFormat(_T("%s (%s)"), pSSentItem->szTypeName, pSSentItem->szType);
                        int nCount = pRowItem->GetCount();
                        CLabelUI* pOption = static_cast<CLabelUI*>(m_pPaintManager->FindControl(_T("word_heading_lb")));
                        pRowItem->GetItemAt(0)->SetText(strValue);
                        pRowItem->GetItemAt(1)->SetText(pSSentItem->szEnSimpleSentence);
                        pRowItem->GetItemAt(2)->SetText(pSSentItem->szZnSimpleSentence);
                    }
                    this->Add(pRowItem);
                }
                
                pRowItem = static_cast<CVerticalLayoutUI*>(m_dlgItemBuilder.Create());

            }
        }

    }

public:
    CPaintManagerUI* m_pPaintManager;
    CDialogBuilder m_dlgItemBuilder;
};